﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using System.Data.OleDb;
using System.Collections;

namespace IService.DataDLL
{
    /// <summary>
    /// 数据库访问的接口层
    /// </summary>
    public class DataAccess
    {
        /// <summary>
        /// 通过BaseInfo类型数据删除
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool Insert(BaseInfo info)
        {
            InsertMethod insert = new InsertMethod(info);
            return insert.Insert();
        }
        /// <summary>
        /// 通过BaseInfo类型数据删除
        /// </summary>
        /// <param name="TableName">数据库表名</param>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool Insert(string TableName, BaseInfo info)
        {
            InsertMethod insert = new InsertMethod(TableName, info);
            return insert.Insert();
        }
        /// <summary>
        /// 查询全部记录
        /// </summary>
        /// <param name="infoType"></param>
        /// <returns></returns>
        public static DataTable SelectAll(Type infoType)
        {
            SelectMethod select = new SelectMethod(infoType);
            return select.Select(null, null);
        }
        /// <summary>
        /// 通过条件查询记录
        /// </summary>
        /// <param name="infoType"></param>
        /// <param name="WhereClause"></param>
        /// <param name="Order"></param>
        /// <returns></returns>
        public static DataTable Select(Type infoType, string WhereClause, string Order)
        {
            SelectMethod select = new SelectMethod(infoType);
            return select.Select(WhereClause, Order);
        }
        /// <summary>
        /// 通过条件和参数查询记录
        /// </summary>
        /// <param name="infoType"></param>
        /// <param name="WhereClause"></param>
        /// <param name="Order"></param>
        /// <returns></returns>
        public static DataTable Select(Type infoType, string WhereClause, string Order, IDataParameter[] paramaters)
        {
            SelectMethod select = new SelectMethod(infoType);
            return select.SelectParam(WhereClause, Order, paramaters);
        }
        /// <summary>
        /// 条件查询前n条记录
        /// </summary>
        /// <param name="infoType"></param>
        /// <param name="TopRow"></param>
        /// <param name="WhereClause"></param>
        /// <param name="Order"></param>
        /// <returns></returns>
        public static DataTable SelectTop(Type infoType, int TopRow, string WhereClause, string Order)
        {
            SelectMethod select = new SelectMethod(infoType);
            return select.SelectTop(TopRow, WhereClause, Order);
        }
        /// <summary>
        /// 查询表前n条记录
        /// </summary>
        /// <param name="infoType"></param>
        /// <param name="TopRow"></param>
        /// <returns></returns>
        public static DataTable SelectTop(Type infoType, int TopRow)
        {
            SelectMethod select = new SelectMethod(infoType);
            return select.SelectTop(TopRow);
        }
        /// <summary>
        /// 传入PaginEntity对象，实现分页查询
        /// </summary>
        /// <param name="infoType"></param>
        /// <param name="pe"></param>
        /// <returns></returns>
        public static DataTable SelectPaging(Type infoType, PagingEntity pe)
        {
            SelectMethod select = new SelectMethod(infoType);
            return select.SelectPaging(pe);
        }
        /// <summary>
        /// 传入一个自增列值，得到一条DataRow类型数据
        /// </summary>
        /// <param name="infoType"></param>
        /// <param name="Identity"></param>
        /// <returns></returns>
        public static DataRow SelectByIdentity(BaseInfo info)
        {
            SelectMethod select = new SelectMethod(info);
            return select.SelectByIdentity();
        }
        /// <summary>
        /// 传入一个自增列值，将BaseInfo数据对象填充
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool SelectInfo(BaseInfo info)
        {
            SelectMethod select = new SelectMethod(info);
            return select.SelectInfoByIdentity();
        }
        /// <summary>
        /// 通过条件查询，将BaseInfo数据对象填充
        /// </summary>
        /// <param name="info"></param>
        /// <param name="WhereClause"></param>
        /// <returns></returns>
        public static bool SelectInfo(BaseInfo info, string WhereClause)
        {
            SelectMethod select = new SelectMethod(info);
            return select.SingleSelect(WhereClause);
        }
        /// <summary>
        /// 通过条件查询，将BaseInfo数据对象填充
        /// </summary>
        /// <param name="info"></param>
        /// <param name="WhereClause"></param>
        /// <returns></returns>
        public static bool SelectInfo(BaseInfo info, string WhereClause, IDataParameter[] paramaters)
        {
            SelectMethod select = new SelectMethod(info);
            return select.SingleSelect(WhereClause, paramaters);
        }
        /// <summary>
        /// 得到表中所有记录行数
        /// </summary>
        /// <param name="infoType"></param>
        /// <returns></returns>
        public static int Count(Type infoType)
        {
            CountMethod count = new CountMethod(infoType);
            return count.Count();
        }
        /// <summary>
        /// 通过条件获得记录行数
        /// </summary>
        /// <param name="infoType"></param>
        /// <param name="WhereClause"></param>
        /// <returns></returns>
        public static int Count(Type infoType, string WhereClause)
        {
            CountMethod count = new CountMethod(infoType);
            return count.Count(WhereClause);
        }
        /// <summary>
        /// 判断指定BaseInfo对象的记录是否存在
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool Exist(BaseInfo info)
        {
            CountMethod count = new CountMethod(info);
            return count.Exists;
        }
        /// <summary>
        /// 删除所有记录（慎重使用）
        /// </summary>
        /// <param name="infoType"></param>
        /// <returns></returns>
        public static bool DeleteAll(Type infoType)
        {
            DeleteMethod delete = new DeleteMethod(infoType);
            return delete.DeleteAll();
        }
        /// <summary>
        /// 通过条件删除数据
        /// </summary>
        /// <param name="infoType"></param>
        /// <param name="WhereClause"></param>
        /// <returns></returns>
        public static bool Delete(Type infoType, string WhereClause)
        {
            DeleteMethod delete = new DeleteMethod(infoType);
            return delete.Delete(WhereClause);
        }
        /// <summary>
        /// 传入一个自增列值，删除某条记录
        /// </summary>
        /// <param name="infoType"></param>
        /// <param name="Identity"></param>
        /// <returns></returns>
        //public static bool DeleteByIdentity(Type infoType, long Identity)
        //{
        //    DeleteMethod delete = new DeleteMethod(infoType);
        //    return delete.DeleteByIdentity(Identity);
        //}
        /// <summary>
        /// 通过传入一个BaseInfo类型对象，删除该对象ID所在表的记录
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool Delete(BaseInfo info)
        {
            DeleteMethod delete = new DeleteMethod(info);
            return delete.DeleteByIdentity();
        }
        /// <summary>
        /// 传入BaseInfo类型对象，更新相应ID的数据库记录
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool Update(BaseInfo info)
        {
            UpdateMethod update = new UpdateMethod(info);
            return update.UpdateByIdentity();
        }
        /// <summary>
        /// 传入一个BaseInfo对象，将其中的数据更新到所有满足条件的记录
        /// </summary>
        /// <param name="info"></param>
        /// <param name="WhereClause"></param>
        /// <returns></returns>
        public static bool Update(BaseInfo info, string WhereClause)
        {
            UpdateMethod update = new UpdateMethod(info);
            return update.Update(WhereClause);
        }
        /// <summary>
        /// 传入Set语句和条件语句，将设置的结果更新到所有满足条件的记录
        /// </summary>
        /// <param name="infoType"></param>
        /// <param name="SetClause"></param>
        /// <param name="WhereClause"></param>
        /// <returns></returns>
        public static bool Update(Type infoType, string SetClause, string WhereClause)
        {
            UpdateMethod update = new UpdateMethod(infoType);
            return update.Update(SetClause, WhereClause, null);
        }
    }
}
